Break the write_dev function out from block into new block-common.sh, where it
authoremellor@leeni.uk.xensource.com <emellor@leeni.uk.xensource.com>
Wed, 26 Oct 2005 13:58:13 +0000 (14:58 +0100)
committeremellor@leeni.uk.xensource.com <emellor@leeni.uk.xensource.com>
Wed, 26 Oct 2005 13:58:13 +0000 (14:58 +0100)
can also be used by block-enbd and block-nbd.

Signed-off-by: Ewan Mellor <ewan@xensource.com>
tools/examples/Makefile
tools/examples/block
tools/examples/block-common.sh [new file with mode: 0644]
tools/examples/block-enbd
tools/examples/block-nbd

index 2af687e535e0d87a9becf44254534abe85828de7..54164235b499c8fc5d99e5b68e6b03734252509a 100644 (file)
@@ -24,8 +24,9 @@ XEN_SCRIPTS = network-bridge vif-bridge
 XEN_SCRIPTS += network-route vif-route
 XEN_SCRIPTS += network-nat vif-nat
 XEN_SCRIPTS += block
-XEN_SCRIPTS += block-enbd
+XEN_SCRIPTS += block-enbd block-nbd
 XEN_SCRIPTS += xen-hotplug-common.sh xen-network-common.sh vif-common.sh
+XEN_SCRIPTS += block-common.sh
 
 XEN_HOTPLUG_DIR = /etc/hotplug
 XEN_HOTPLUG_SCRIPTS = xen-backend.agent
index 2e383bb3a660e82aae308046b9793b533d907b0d..c5b6b6c215688bdaaf7e57df8bd67d58af30eca7 100644 (file)
@@ -1,7 +1,7 @@
 #!/bin/sh
 
 dir=$(dirname "$0")
-. "$dir/xen-hotplug-common.sh"
+. "$dir/block-common.sh"
 
 expand_dev() {
   local dev
@@ -16,21 +16,9 @@ expand_dev() {
   echo -n $dev
 }
 
-write_dev() {
-  local major
-  local minor
-  local pdev
-       
-  major=$(stat -L -c %t "$1")
-  minor=$(stat -L -c %T "$1")
-  pdev=$(printf "0x%02x%02x" 0x$major 0x$minor)
-  xenstore_write "$XENBUS_PATH"/physical-device $pdev \
-      "$XENBUS_PATH"/node $1
-}
-
 t=$(xenstore_read "$XENBUS_PATH"/type || true)
 
-case $1 in 
+case "$command" in 
   bind)
     p=$(xenstore_read "$XENBUS_PATH"/params)
     case $t in 
diff --git a/tools/examples/block-common.sh b/tools/examples/block-common.sh
new file mode 100644 (file)
index 0000000..1d20550
--- /dev/null
@@ -0,0 +1,51 @@
+#
+# Copyright (c) 2005 XenSource Ltd.
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of version 2.1 of the GNU Lesser General Public
+# License as published by the Free Software Foundation.
+#
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+#
+
+
+dir=$(dirname "$0")
+. "$dir/xen-hotplug-common.sh"
+
+command="$1"
+
+if [ "$command" != "bind" ] && [ "$command" != "unbind" ]
+then
+  log err "Invalid command: $command"
+  exit 1
+fi
+
+
+XENBUS_PATH="${XENBUS_PATH:?}"
+
+
+##
+# Write physical-device = 0xMMmm and node = device to the store, where MM
+# and mm are the major and minor numbers of device.
+#
+# @param device The device from which major and minor numbers are read, which
+#               will be written into the store.
+#
+write_dev() {
+  local major
+  local minor
+  local pdev
+       
+  major=$(stat -L -c %t "$1")
+  minor=$(stat -L -c %T "$1")
+  pdev=$(printf "0x%02x%02x" "0x$major" "0x$minor")
+  xenstore_write "$XENBUS_PATH"/physical-device "$pdev" \
+                 "$XENBUS_PATH"/node "$1"
+}
index c86068d817b37dcb95ea5a60025bfb5320899d3f..c58adb595d5174ef9bcc7740d7f32dc8fec7cad9 100755 (executable)
@@ -2,25 +2,19 @@
 
 # Usage: block-enbd [bind server ctl_port |unbind node]
 #
-# The file argument to the bind command is the file we are to bind to a
-# loop device.
-#
 # The node argument to unbind is the name of the device node we are to
 # unbind.
 #
 # This assumes you're running a correctly configured server at the other end!
 
-set -e
+dir=$(dirname "$0")
+. "$dir/block-common.sh"
 
-case $1 in
+case "$command" in
   bind)
     for dev in /dev/nd*; do
       if nbd-client $2:$3 $dev; then
-        major=$(stat -L -c %t "$dev")
-        minor=$(stat -L -c %T "$dev")
-        pdev=$(printf "0x%02x%02x" 0x$major 0x$minor)
-        xenstore-write "$XENBUS_PATH"/physical-device $pdev \
-          "$XENBUS_PATH"/node $dev
+        write_dev $dev
         exit 0
       fi
     done
index 06f9f6875e85e5a3cbf4d37b78a3f5676c44a41c..142a403c0f97e60a621d80cd1a02bd070b90b786 100644 (file)
@@ -1,28 +1,20 @@
 #!/bin/sh
 
-# Usage: block-enbd [bind server ctl_port |unbind node]
-#
-# The file argument to the bind command is the file we are to bind to a
-# loop device.
+# Usage: block-nbd [bind server ctl_port |unbind node]
 #
 # The node argument to unbind is the name of the device node we are to
 # unbind.
 #
 # This assumes you're running a correctly configured server at the other end!
 
-set -e
-
-#echo "block-enbd: $@" | logger -t xen
+dir=$(dirname "$0")
+. "$dir/block-common.sh"
 
-case $1 in
+case "$command" in
   bind)
     for dev in /dev/nbd*; do
       if nbd-client $2 $3 $dev; then
-        major=$(stat -L -c %t "$dev")
-        minor=$(stat -L -c %T "$dev")
-        pdev=$(printf "0x%02x%02x" 0x$major 0x$minor)
-        xenstore-write "$XENBUS_PATH"/physical-device $pdev \
-          "$XENBUS_PATH"/node $dev
+        write_dev $dev
         exit 0
       fi
     done